package com.chen.spikemall.service.impl;

import com.chen.spikemall.bean.CommonGoods;
import com.chen.spikemall.exception.NoInventoryException;
import com.chen.spikemall.repository.GoodsRepository;
import com.chen.spikemall.repository.GoodsStoreValueRepository;
import com.chen.spikemall.service.GoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GoodsServiceImpl implements GoodsService {

    private static final Logger logger = LoggerFactory.getLogger(GoodsServiceImpl.class);

    @Autowired
    private GoodsRepository goodsRepository;

    @Autowired
    private GoodsStoreValueRepository goodsStoreValueRepository;

    @Autowired
    private OrderServiceImpl orderService;

    @Override
    public void addGoods(CommonGoods commonGoods, int totalStoreCount) {
        goodsRepository.save(commonGoods);
        goodsStoreValueRepository.setStoreCount(commonGoods.getGoodsNo(), totalStoreCount);
    }

    @Override
    public void goodsStoreValueDecrease(String goodsNo) throws NoInventoryException {
        try {
            int store = goodsStoreValueRepository.decrStoreCount(goodsNo, 1);
            logger.info(" goodsNo : 当前库存 " + store);

//            orderService.createOrder(goodsNo);

        } catch (NoInventoryException e) {
            throw e;
        }
    }
}
